package com.ruoyi.lxy.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lxy.domain.vo.TSelectUserVo;
import com.ruoyi.lxy.domain.bo.TSelectUserBo;
import com.ruoyi.lxy.service.ITSelectUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户选择
 *
 * @author ruoyi
 * @date 2023-10-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lxy/selectUser")
public class TSelectUserController extends BaseController {

    private final ITSelectUserService iTSelectUserService;

    /**
     * 查询用户选择列表
     */
    @SaCheckPermission("lxy:selectUser:list")
    @GetMapping("/list")
    public TableDataInfo<TSelectUserVo> list(TSelectUserBo bo, PageQuery pageQuery) {
        return iTSelectUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户选择列表
     */
    @SaCheckPermission("lxy:selectUser:export")
    @Log(title = "用户选择", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TSelectUserBo bo, HttpServletResponse response) {
        List<TSelectUserVo> list = iTSelectUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户选择", TSelectUserVo.class, response);
    }

    /**
     * 获取用户选择详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("lxy:selectUser:query")
    @GetMapping("/{id}")
    public R<TSelectUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTSelectUserService.queryById(id));
    }

    /**
     * 新增用户选择
     */
    @SaCheckPermission("lxy:selectUser:add")
    @Log(title = "用户报名", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TSelectUserBo bo) {
        return toAjax(iTSelectUserService.insertByBo(bo));
    }

    /**
     * 修改用户选择
     */
    @SaCheckPermission("lxy:selectUser:edit")
    @Log(title = "用户选择", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TSelectUserBo bo) {
        return toAjax(iTSelectUserService.updateByBo(bo));
    }

    /**
     * 删除用户选择
     *
     * @param ids 主键串
     */

    @SaCheckRole(value ={"admin","common_admin"} ,mode = SaMode.OR)
    @Log(title = "用户选择", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTSelectUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @SaCheckRole(value ={"admin","common_admin"} ,mode = SaMode.OR)
    @Log(title = "用户退选", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userId}/{timeId}")
    public R<Void> remove(@PathVariable Long userId,@PathVariable Long timeId) {
        return toAjax(iTSelectUserService.deleteWithValidById(userId,timeId,true));
    }

}
